<?php

function smarty_function_html_admin_calendar($params, &$smarty)
{
	$name = 'date';
	$id = '';
	$value = '';
	$prefix = '';
	$class = '';
	$date = '';
	$trigger = 'ico-cal-li.gif';
	
	foreach ($params as $_key => $_value)
	{
        switch ($_key)
        {
            case 'name': $name = (string)$_value; break;
            case 'value': if (is_numeric($_value)) { $value = $_value; } break;
            case 'class': $class = (string)$_value; break;
            case 'trigger': $trigger = (string)$_value; break;
            case 'prefix': $prefix = (string)$_value; break;
            case 'id': $id = (string)$_value; break;
        }
	}

	$safe_name = str_replace(array('-','>','<','+'), '_', $name);
	
	if (!empty($value))
	{
		$date = date('d.m.Y', $value);
	}
	
	$html_result = '<input type="text" id="'.$prefix.$name.'_visible" value="'.$date.'" class="'.$class.'" onChange="'.$prefix.'changeDate'.$safe_name.'(); return true" style="width:65px" />';
	$html_result .= ' <img style="cursor:pointer;" align="absmiddle" src="/application/views/scripts/client/images/'.$trigger.'" id="'.$prefix.$name.'_trigger" style="vertical-align:bottom" />';
	$html_result .= '<script type="text/javascript">//<![CDATA[
	      Zapatec.Calendar.setup({
	        firstDay          : 1,
	        weekNumbers       : true,
	        showOthers        : false,
	        showsTime         : false,
	        timeFormat        : "24",
	        step              : 2,
	        range             : [1900.01, 2999.12],
	        electric          : false,
	        singleClick       : true,
	        inputField        : "'.$prefix.$name.'_visible",
	        button            : "'.$prefix.$name.'_trigger",
	        ifFormat          : "%d.%m.%Y",
	        daFormat          : "%d.%m.%Y",
	        align             : "Br"
	      });
	    //]]>
    	function '.$prefix.'changeDate'.$safe_name.'()
    	{
	    	var date_value = document.getElementById(\''.$prefix.$name.'_visible\').value;
    		var day = parseInt(date_value.substring(0, 2));
    		var month = parseInt(date_value.substring(3, 5));
    		var yeear = parseInt(date_value.substring(6));
			var humDate = new Date(Date.UTC(yeear, month-1, day, 0, 0, 0));
			var time_stamp = humDate.getTime()/1000.0;
			document.getElementById(\''.$prefix.$name.$id.'\').value = time_stamp;
		}
	    </script>';
	$html_result .= '<input type="hidden" id="'.$prefix.$name.$id.'" name="'.$name.'" value="'.$value.'">';
	
	return $html_result;
}

?>